#include "WidgetMainWindow.h"
#include "DefineCommon.h"
#include "ECommandManager.h"
#include "qdatetime.h"
#include "ui_WidgetMainWindow.h"
#include <iostream>
#include <TabToolbar/TabToolbar.h>
#include <TabToolbar/Page.h>
#include <TabToolbar/Group.h>
#include <QDockWidget>
#include <QDir>
#include <WidgetsManager.h>
#include <QFileDialog>
#include <QFileInfo>
#include <QMenu>

WidgetMainWindow::WidgetMainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::WidgetMainWindow)
{
    ui->setupUi(this);
    
    
    //setup tabToolbar:
    tt::TabToolbar* ttbar = new tt::TabToolbar(this, 75, 3);
    addToolBar(Qt::TopToolBarArea, ttbar);
    
    tt::Page* pageCommon =  ttbar->AddPage("General");
    
    tt::Group* groupFile = pageCommon->AddGroup("File");
    QMenu* menuOpenFile = new QMenu(this);
    menuOpenFile->setObjectName("OpenFile");
    menuOpenFile->addAction(ui->actionOpenFileNormal);

    groupFile->AddAction(QToolButton::ToolButtonPopupMode::InstantPopup, ui->actionOpenFile, menuOpenFile);
    groupFile->AddAction(QToolButton::ToolButtonPopupMode::InstantPopup, ui->actionNewFile);
    groupFile->AddAction(QToolButton::ToolButtonPopupMode::InstantPopup, ui->actionSaveFile);

    tt::Group* groupRedoUndo = pageCommon->AddGroup("RedoUndo");
    groupRedoUndo->AddAction(QToolButton::ToolButtonPopupMode::InstantPopup
            , ui->actionUndo 
            );
    groupRedoUndo->AddAction(QToolButton::ToolButtonPopupMode::InstantPopup
            , ui->actionRedo
            );
    // QMenu* menuRedoUndo = new QMenu(this);
    // menuRedoUndo->setObjectName("RedoUndo");
    // menuRedoUndo->addAction(ui->actionUndo);
    // menuRedoUndo->addAction(ui->actionRedo);

    QMenu* menuExportFile = new QMenu(this);
    menuExportFile->setObjectName("ExportFile");
    menuExportFile->addAction(ui->actionExport);
    groupFile->AddAction(QToolButton::ToolButtonPopupMode::InstantPopup, ui->actionExport, menuExportFile);

    groupFile->AddAction(QToolButton::ToolButtonPopupMode::InstantPopup, ui->actionTypeList);

    tt::Group* groupSim = pageCommon->AddGroup("Simulation");
    // groupSim->AddAction(QToolButton::ToolButtonPopupMode::InstantPopup, ui->actionRun);
    groupSim->AddAction(QToolButton::ToolButtonPopupMode::InstantPopup, ui->actionPlay);
    groupSim->AddAction(QToolButton::ToolButtonPopupMode::InstantPopup, ui->actionPause);
    groupSim->AddAction(QToolButton::ToolButtonPopupMode::InstantPopup, ui->actionStop);
    // groupSim->AddAction(QToolButton::ToolButtonPopupMode::InstantPopup, ui->actionResult);


    tt::Group* groupData = pageCommon->AddGroup("Data");
    groupData->AddAction(QToolButton::ToolButtonPopupMode::InstantPopup, ui->actionDataMonitor);

    tt::Group* groupReflection = pageCommon->AddGroup("Meta");
    groupReflection->AddAction(QToolButton::ToolButtonPopupMode::InstantPopup, ui->actionReflection);

    tt::Group* groupPlugin = pageCommon->AddGroup("Plugin");
    groupPlugin->AddAction(QToolButton::ToolButtonPopupMode::InstantPopup, ui->actionPlugin);


    groupReflection->AddAction(QToolButton::ToolButtonPopupMode::InstantPopup, ui->actionReflection);
    //add some TabToolbar's ptr to widgetsManager, let other plugin-liked dll use these to custom main window
    WidgetsManager::instance()->addWidgetToMap("TabToolBar", ttbar);
    WidgetsManager::instance()->addWidgetToMap("PageCommon", pageCommon);
    WidgetsManager::instance()->addWidgetToMap("GroupFile", groupFile);
    WidgetsManager::instance()->addWidgetToMap("MenuOpenFile", menuOpenFile);
    WidgetsManager::instance()->addWidgetToMap("MenuExportFile", menuExportFile);
    WidgetsManager::instance()->addWidgetToMap("GroupSim", groupSim);
    WidgetsManager::instance()->addWidgetToMap("GroupData", groupData);
    WidgetsManager::instance()->addWidgetToMap("GroupData", groupReflection);


    QObject::connect(ui->actionTypeList, 
                     SIGNAL(triggered(bool)),
                     this, 
                     SLOT(slotActionShowTypeList(bool))
                     );

    QObject::connect(ui->actionOpenFileNormal, 
                     SIGNAL(triggered(bool)),
                     this, 
                     SLOT(slotActionOpenFileNormal(bool))
                     );

    QObject::connect(ui->actionSaveFile, 
                     SIGNAL(triggered(bool)),
                     this, 
                     SLOT(slotActionSaveFile(bool))
                     );

    QObject::connect(ui->actionDataMonitor, 
                     SIGNAL(triggered(bool)),
                     this, 
                     SLOT(slotActionDataMonitor(bool))
                     );
	
    QObject::connect(ui->actionReflection, 
                     SIGNAL(triggered(bool)),
                     this, 
                     SLOT(slotActionReflection(bool))
                     );

    QObject::connect(ui->actionPlugin, 
                     SIGNAL(triggered(bool)),
                     this, 
                     SLOT(slotActionPlugin(bool))
                     );

    QObject::connect(ui->actionUndo, 
                     SIGNAL(triggered(bool)),
                     this, 
                     SLOT(slotActionUndo(bool))
                     );
    QObject::connect(ui->actionRedo, 
                     SIGNAL(triggered(bool)),
                     this, 
                     SLOT(slotActionRedo(bool))
                     );
    this->setGeometry(0,0,1000,500);


    this->m_timerECommandManager = new QTimer;
    QObject::connect(this->m_timerECommandManager, 
                     SIGNAL(timeout()),
                     this, 
                     SLOT(slotTimerOutECommandManager())
                     );
    this->m_timerECommandManager->start(10);
}

WidgetMainWindow::~WidgetMainWindow()
{
    delete ui;
}


void WidgetMainWindow::setWidgetToDockLeft(QWidget* widgetPtr)
{
    ui->dockWidgetLeft->setWidget(widgetPtr);
}

void WidgetMainWindow::setWidgetToDockRight(QWidget* widgetPtr)
{
    ui->dockWidgetRight->setWidget(widgetPtr);
}

void WidgetMainWindow::setWidgetToDockDown(QWidget* widgetPtr)
{
    ui->dockWidgetDown->setWidget(widgetPtr);
}

QAction* WidgetMainWindow::getAction(const QString& name)
{
	QAction* actionPtr = NULL;

	return actionPtr;
}

void WidgetMainWindow::slotActionFileOpen(bool value)
{

}
    
void WidgetMainWindow::slotActionOpenFileNormal(bool value)
{
    QString fileName = QFileDialog::getOpenFileName(this, "读取","");
    if(fileName.isEmpty() == false)
    {
        emit signalOpenNormal(fileName);
        // emit signalAfterReadFile();
    }
}


void WidgetMainWindow::slotActionSaveFile(bool value)
{
    QString fileName = QFileDialog::getSaveFileName(this,
                                 "保存",
                                 "",
                                 "");
}



void WidgetMainWindow::slotActionShowTypeList(bool value)
{
    QDialog* widgetPtr = dynamic_cast<QDialog*>(WidgetsManager::instance()->getWidgetFromMap("DialogTypeList"));
    if(widgetPtr != nullptr)
    {
        widgetPtr->exec();
    }

}


void WidgetMainWindow::slotActionDataMonitor(bool value)
{
    QDialog* widgetPtr = dynamic_cast<QDialog*>(WidgetsManager::instance()->getWidgetFromMap("DialogDataMonitor"));
    if(widgetPtr != nullptr)
    {
        widgetPtr->exec();
    }
}

void WidgetMainWindow::slotActionReflection(bool value)
{
    QDialog* widgetPtr = dynamic_cast<QDialog*>(WidgetsManager::instance()->getWidgetFromMap("DialogReflection"));
    if(widgetPtr != nullptr)
    {
        widgetPtr->exec();
    }
}

void WidgetMainWindow::slotActionPlugin(bool value)
{
    QDialog* widgetPtr = dynamic_cast<QDialog*>(WidgetsManager::instance()->getWidgetFromMap("DialogPlugin"));
    if(widgetPtr != nullptr)
    {
        widgetPtr->exec();
    }
}

void WidgetMainWindow::slotActionUndo(bool value)
{
    // ELOG_DEBUG("undo...");
    ECommandManager::instance()->processECommandUndoOnce();
}

void WidgetMainWindow::slotActionRedo(bool value)
{
    // ELOG_DEBUG("redo...");
    ECommandManager::instance()->processECommandRedoOnce();
}

void WidgetMainWindow::slotTimerOutECommandManager()
{
    ECommandManager::instance()->processECommand();
}
